home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / DINOBALL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  8.6 KB  |  333 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994.  */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <math.h>       /* for cos(), sin(), and sqrt() */
  12. #include <GL/glut.h>
  13.  
  14. typedef enum {
  15.   RESERVED, BODY_SIDE, BODY_EDGE, BODY_WHOLE, ARM_SIDE, ARM_EDGE, ARM_WHOLE,
  16.   LEG_SIDE, LEG_EDGE, LEG_WHOLE, EYE_SIDE, EYE_EDGE, EYE_WHOLE, DINOSAUR
  17. } displayLists;
  18.  
  19. GLfloat angle = -150;   /* in degrees */
  20. GLfloat xloc = 0, yloc = 0, zloc = 0;
  21. int moving, begin;
  22. int W = 300, H = 300;
  23. GLdouble bodyWidth = 3.0;
  24. int newModel = 1;
  25. /* *INDENT-OFF* */
  26. GLfloat body[][2] = { {0, 3}, {1, 1}, {5, 1}, {8, 4}, {10, 4}, {11, 5},
  27.   {11, 11.5}, {13, 12}, {13, 13}, {10, 13.5}, {13, 14}, {13, 15}, {11, 16},
  28.   {8, 16}, {7, 15}, {7, 13}, {8, 12}, {7, 11}, {6, 6}, {4, 3}, {3, 2},
  29.   {1, 2} };
  30. GLfloat arm[][2] = { {8, 10}, {9, 9}, {10, 9}, {13, 8}, {14, 9}, {16, 9},
  31.   {15, 9.5}, {16, 10}, {15, 10}, {15.5, 11}, {14.5, 10}, {14, 11}, {14, 10},
  32.   {13, 9}, {11, 11}, {9, 11} };
  33. GLfloat leg[][2] = { {8, 6}, {8, 4}, {9, 3}, {9, 2}, {8, 1}, {8, 0.5}, {9, 0},
  34.   {12, 0}, {10, 1}, {10, 2}, {12, 4}, {11, 6}, {10, 7}, {9, 7} };
  35. GLfloat eye[][2] = { {8.75, 15}, {9, 14.7}, {9.6, 14.7}, {10.1, 15},
  36.   {9.6, 15.25}, {9, 15.25} };
  37. GLfloat lightZeroPosition[] = {10.0, 4.0, 10.0, 1.0};
  38. GLfloat lightZeroColor[] = {0.8, 1.0, 0.8, 1.0}; /* green-tinted */
  39. GLfloat lightOnePosition[] = {-1.0, -2.0, 1.0, 0.0};
  40. GLfloat lightOneColor[] = {0.6, 0.3, 0.2, 1.0}; /* red-tinted */
  41. GLfloat skinColor[] = {0.1, 1.0, 0.1, 1.0}, eyeColor[] = {1.0, 0.2, 0.2, 1.0};
  42. /* *INDENT-ON* */
  43.  
  44. void
  45. extrudeSolidFromPolygon(GLfloat data[][2], unsigned int dataSize,
  46.   GLdouble thickness, GLuint side, GLuint edge, GLuint whole)
  47. {
  48.   static GLUtriangulatorObj *tobj = NULL;
  49.   GLdouble vertex[3], dx, dy, len;
  50.   int i;
  51.   int count = dataSize / (int) (2 * sizeof(GLfloat));
  52.  
  53.   if (tobj == NULL) {
  54.     tobj = gluNewTess();  /* create and initialize a GLU
  55.                              polygon tesselation object */
  56.     gluTessCallback(tobj, GLU_BEGIN, glBegin);
  57.     gluTessCallback(tobj, GLU_VERTEX, glVertex2fv);  /* semi-tricky */
  58.     gluTessCallback(tobj, GLU_END, glEnd);
  59.   }
  60.   glNewList(side, GL_COMPILE);
  61.   glShadeModel(GL_SMOOTH);  /* smooth minimizes seeing
  62.                                tessellation */
  63.   gluBeginPolygon(tobj);
  64.   for (i = 0; i < count; i++) {
  65.     vertex[0] = data[i][0];
  66.     vertex[1] = data[i][1];
  67.     vertex[2] = 0;
  68.     gluTessVertex(tobj, vertex, data[i]);
  69.   }
  70.   gluEndPolygon(tobj);
  71.   glEndList();
  72.   glNewList(edge, GL_COMPILE);
  73.   glShadeModel(GL_FLAT);  /* flat shade keeps angular hands
  74.                              from being "smoothed" */
  75.   glBegin(GL_QUAD_STRIP);
  76.   for (i = 0; i <= count; i++) {
  77.     /* mod function handles closing the edge */
  78.     glVertex3f(data[i % count][0], data[i % count][1], 0.0);
  79.     glVertex3f(data[i % count][0], data[i % count][1], thickness);
  80.     /* Calculate a unit normal by dividing by Euclidean
  81.        distance. We * could be lazy and use
  82.        glEnable(GL_NORMALIZE) so we could pass in * arbitrary
  83.        normals for a very slight performance hit. */
  84.     dx = data[(i + 1) % count][1] - data[i % count][1];
  85.     dy = data[i % count][0] - data[(i + 1) % count][0];
  86.     len = sqrt(dx * dx + dy * dy);
  87.     glNormal3f(dx / len, dy / len, 0.0);
  88.   }
  89.   glEnd();
  90.   glEndList();
  91.   glNewList(whole, GL_COMPILE);
  92.   glFrontFace(GL_CW);
  93.   glCallList(edge);
  94.   glNormal3f(0.0, 0.0, -1.0);  /* constant normal for side */
  95.   glCallList(side);
  96.   glPushMatrix();
  97.   glTranslatef(0.0, 0.0, thickness);
  98.   glFrontFace(GL_CCW);
  99.   glNormal3f(0.0, 0.0, 1.0);  /* opposite normal for other side */
  100.   glCallList(side);
  101.   glPopMatrix();
  102.   glEndList();
  103. }
  104.  
  105. void
  106. makeDinosaur(void)
  107. {
  108.   extrudeSolidFromPolygon(body, sizeof(body), bodyWidth,
  109.     BODY_SIDE, BODY_EDGE, BODY_WHOLE);
  110.   extrudeSolidFromPolygon(arm, sizeof(arm), bodyWidth / 4,
  111.     ARM_SIDE, ARM_EDGE, ARM_WHOLE);
  112.   extrudeSolidFromPolygon(leg, sizeof(leg), bodyWidth / 2,
  113.     LEG_SIDE, LEG_EDGE, LEG_WHOLE);
  114.   extrudeSolidFromPolygon(eye, sizeof(eye), bodyWidth + 0.2,
  115.     EYE_SIDE, EYE_EDGE, EYE_WHOLE);
  116.   glNewList(DINOSAUR, GL_COMPILE);
  117.   glMaterialfv(GL_FRONT, GL_DIFFUSE, skinColor);
  118.   glCallList(BODY_WHOLE);
  119.   glPushMatrix();
  120.   glTranslatef(0.0, 0.0, bodyWidth);
  121.   glCallList(ARM_WHOLE);
  122.   glCallList(LEG_WHOLE);
  123.   glTranslatef(0.0, 0.0, -bodyWidth - bodyWidth / 4);
  124.   glCallList(ARM_WHOLE);
  125.   glTranslatef(0.0, 0.0, -bodyWidth / 4);
  126.   glCallList(LEG_WHOLE);
  127.   glTranslatef(0.0, 0.0, bodyWidth / 2 - 0.1);
  128.   glMaterialfv(GL_FRONT, GL_DIFFUSE, eyeColor);
  129.   glCallList(EYE_WHOLE);
  130.   glPopMatrix();
  131.   glEndList();
  132. }
  133.  
  134. void
  135. recalcModelView(void)
  136. {
  137.   glPopMatrix();
  138.   glPushMatrix();
  139.   glTranslatef(xloc, yloc, zloc);
  140.   glRotatef(angle, 0.0, 1.0, 0.0);
  141.   glTranslatef(-8, -8, -bodyWidth / 2);
  142.   newModel = 0;
  143. }
  144.  
  145. void
  146. redraw(void)
  147. {
  148.   if (newModel)
  149.     recalcModelView();
  150.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  151.   glCallList(DINOSAUR);
  152.   glutSwapBuffers();
  153. }
  154.  
  155. /* ARGSUSED3 */
  156. void
  157. mouse(int button, int state, int x, int y)
  158. {
  159.   if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  160.     moving = 1;
  161.     begin = x;
  162.   }
  163.   if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
  164.     moving = 0;
  165.   }
  166. }
  167.  
  168. /* ARGSUSED1 */
  169. void
  170. motion(int x, int y)
  171. {
  172.   if (moving) {
  173.     angle = angle + (x - begin);
  174.     begin = x;
  175.     newModel = 1;
  176.     glutPostRedisplay();
  177.   }
  178. }
  179.  
  180. void
  181. tablet(int x, int y)
  182. {
  183.   xloc = ((GLfloat) x) / 500 - 4;
  184.   yloc = ((GLfloat) y) / 1000 - 2;
  185.   newModel = 1;
  186.   glutPostRedisplay();
  187. }
  188.  
  189. int xt = 1, yt = 1, zt = 1, xr = 1;
  190.  
  191. void
  192. translate(int x, int y, int z)
  193. {
  194.   GLfloat newz;
  195.  
  196.   if (xt)
  197.     xloc += ((GLfloat) x) / 100;
  198.   if (yt)
  199.     yloc += ((GLfloat) y) / 100;
  200.   if (zt) {
  201.     newz = zloc - ((GLfloat) z) / 100;
  202.     if (newz > -60.0 && newz < 13.0)
  203.       zloc = newz;
  204.   }
  205.   newModel = 1;
  206.   glutPostRedisplay();
  207. }
  208.  
  209. /* ARGSUSED1 */
  210. void
  211. rotate(int x, int y, int z)
  212. {
  213.   if (xr) {
  214.     angle += x / 2.0;
  215.     newModel = 1;
  216.     glutPostRedisplay();
  217.   }
  218. }
  219.  
  220. void
  221. button(int button, int state)
  222. {
  223.   if (state == GLUT_DOWN) {
  224.     switch (button) {
  225.     case 1:
  226.       xt = yt = zt = xr = 1;
  227.       break;
  228.     case 5:
  229.       xt = 1;
  230.       yt = zt = xr = 0;
  231.       break;
  232.     case 6:
  233.       yt = 1;
  234.       xt = zt = xr = 0;
  235.       break;
  236.     case 7:
  237.       zt = 1;
  238.       xt = yt = xr = 0;
  239.       break;
  240.     case 8:
  241.       xr = 1;
  242.       xt = yt = zt = 0;
  243.       break;
  244.     case 9:
  245.       xloc = yloc = zloc = 0;
  246.       newModel = 1;
  247.       glutPostRedisplay();
  248.       break;
  249.     }
  250.   }
  251. }
  252.  
  253. void
  254. dials(int dial, int value)
  255. {
  256.   if (dial == 0) {
  257.     angle = value / 10.0;
  258.     newModel = 1;
  259.     glutPostRedisplay();
  260.   }
  261. }
  262.  
  263. GLboolean lightZeroSwitch = GL_TRUE, lightOneSwitch = GL_TRUE;
  264.  
  265. void
  266. controlLights(int value)
  267. {
  268.   switch (value) {
  269.   case 1:
  270.     lightZeroSwitch = !lightZeroSwitch;
  271.     if (lightZeroSwitch) {
  272.       glEnable(GL_LIGHT0);
  273.     } else {
  274.       glDisable(GL_LIGHT0);
  275.     }
  276.     break;
  277.   case 2:
  278.     lightOneSwitch = !lightOneSwitch;
  279.     if (lightOneSwitch) {
  280.       glEnable(GL_LIGHT1);
  281.     } else {
  282.       glDisable(GL_LIGHT1);
  283.     }
  284.     break;
  285.   }
  286.   glutPostRedisplay();
  287. }
  288.  
  289. int
  290. main(int argc, char **argv)
  291. {
  292.   glutInit(&argc, argv);
  293.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  294.   glutCreateWindow("dinoball (Spaceball demo)");
  295.   glutDisplayFunc(redraw);
  296.   glutMouseFunc(mouse);
  297.   glutMotionFunc(motion);
  298.   glutTabletMotionFunc(tablet);
  299.   glutSpaceballMotionFunc(translate);
  300.   glutSpaceballRotateFunc(rotate);
  301.   glutSpaceballButtonFunc(button);
  302.   glutDialsFunc(dials);
  303.   glutCreateMenu(controlLights);
  304.   glutAddMenuEntry("Toggle right light", 1);
  305.   glutAddMenuEntry("Toggle left light", 2);
  306.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  307.   makeDinosaur();
  308.   glEnable(GL_CULL_FACE);
  309.   glEnable(GL_DEPTH_TEST);
  310.   glEnable(GL_LIGHTING);
  311.   glMatrixMode(GL_PROJECTION);
  312.   gluPerspective( /* field of view in degree */ 40.0,
  313.   /* aspect ratio */ 1.0,
  314.     /* Z near */ 1.0, /* Z far */ 100.0);
  315.   glMatrixMode(GL_MODELVIEW);
  316.   gluLookAt(0.0, 0.0, 30.0,  /* eye is at (0,0,30) */
  317.     0.0, 0.0, 0.0,      /* center is at (0,0,0) */
  318.     0.0, 1.0, 0.);      /* up is in positive Y direction */
  319.   glPushMatrix();       /* dummy push so we can pop on model
  320.                            recalc */
  321.   glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  322.   glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition);
  323.   glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor);
  324.   glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1);
  325.   glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05);
  326.   glLightfv(GL_LIGHT1, GL_POSITION, lightOnePosition);
  327.   glLightfv(GL_LIGHT1, GL_DIFFUSE, lightOneColor);
  328.   glEnable(GL_LIGHT0);
  329.   glEnable(GL_LIGHT1);
  330.   glutMainLoop();
  331.   return 0;             /* ANSI C requires main to return int. */
  332. }
  333.